package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.text.Convert;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ReAccount;
import com.ruoyi.system.service.IReAccountService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * f分销积分记录Controller
 * 
 * @author ruoyi
 * @date 2023-12-27
 */
@Controller
@RequestMapping("/system/account")
public class ReAccountController extends BaseController
{
    private String prefix = "system/account";

    @Autowired
    private IReAccountService reAccountService;

    @RequiresPermissions("system:account:view")
    @GetMapping()
    public String account()
    {
        return prefix + "/account";
    }

    @RequiresPermissions("system:account:view")
    @GetMapping("/openScoring")
    public String openScoring()
    {
        return prefix + "/scoring";
    }

    @RequiresPermissions("system:account:view")
    @GetMapping("/openScoringed")
    public String openScoringed()
    {
        return prefix + "/scoringed";
    }

    /**
     * 查询f分销积分记录列表
     */
    @RequiresPermissions("system:account:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ReAccount reAccount)
    {
        startPage();
        List<ReAccount> list = reAccountService.selectReAccountList(reAccount);
        return getDataTable(list);
    }


    /**
     * 个人奖励积分提现
     */
    @RequiresPermissions("system:account:list")
    @PostMapping("/scoringList")
    @ResponseBody
    public TableDataInfo scoringList(ReAccount reAccount)
    {
        reAccount.setCreateBy(getLoginName());
        reAccount.setCashState("0");
        List<ReAccount> list = reAccountService.selectReAccountList(reAccount);
        return getDataTable(list);
    }

    /**
     * 个人奖励积分提现
     */
    @RequiresPermissions("system:account:list")
    @PostMapping("/scoringedList")
    @ResponseBody
    public TableDataInfo scoringedList(ReAccount reAccount)
    {
        reAccount.setCreateBy(getLoginName());
        reAccount.setCashState("1");
        startPage();
        List<ReAccount> list = reAccountService.selectReAccountList(reAccount);
        return getDataTable(list);
    }

    /**
     * 导出f分销积分记录列表
     */
    @RequiresPermissions("system:account:export")
    @Log(title = "f分销积分记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ReAccount reAccount)
    {
        List<ReAccount> list = reAccountService.selectReAccountList(reAccount);
        ExcelUtil<ReAccount> util = new ExcelUtil<ReAccount>(ReAccount.class);
        return util.exportExcel(list, "f分销积分记录数据");
    }

    /**
     * 新增f分销积分记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存f分销积分记录
     */
    @RequiresPermissions("system:account:add")
    @Log(title = "f分销积分记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ReAccount reAccount)
    {
        return toAjax(reAccountService.insertReAccount(reAccount));
    }

    /**
     * 修改f分销积分记录
     */
    @RequiresPermissions("system:account:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ReAccount reAccount = reAccountService.selectReAccountById(id);
        mmap.put("reAccount", reAccount);
        return prefix + "/edit";
    }

    /**
     * 修改保存f分销积分记录
     */
    @RequiresPermissions("system:account:edit")
    @Log(title = "f分销积分记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ReAccount reAccount)
    {
        return toAjax(reAccountService.updateReAccount(reAccount));
    }

    /**
     * 删除f分销积分记录
     */
    @RequiresPermissions("system:account:remove")
    @Log(title = "f分销积分记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(reAccountService.deleteReAccountByIds(ids));
    }

    /**
     * 分销积分提现
     */
    @RequiresPermissions("system:account:cash")
    @Log(title = "分销积分提现", businessType = BusinessType.UPDATE)
    @PostMapping( "/cash")
    @ResponseBody
    public AjaxResult cash(String ids)
    {
        Long[] accountIds = Convert.toLongArray(ids);
        for (Long id : accountIds)
        {
            ReAccount reAccount=reAccountService.selectReAccountById(id);
            reAccount.setCashState("1");
            reAccountService.updateReAccount(reAccount);

        }

        return toAjax(accountIds.length);
    }
}
